/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scrumpmt.web;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;
import org.scrumpmt.domain.Colaborador;

/**
 *
 * @author Cristian
 */
@ManagedBean(name = "burndownBean")
@RequestScoped
public class burndownBean extends BasicBean {

    private PieChartModel pieModel;
    private PieChartModel pieModeltodo;
    private PieChartModel pieModeldoing;
    private PieChartModel pieModeldone;
    private CartesianChartModel burndownModel;
    FacesContext context = FacesContext.getCurrentInstance();
    ProductoSeleccionadoBean bean = (ProductoSeleccionadoBean) context.getApplication().
            evaluateExpressionGet(context, "#{productoSeleccionadoBean}", ProductoSeleccionadoBean.class);
    estadistica estad = (estadistica) context.getApplication().
            evaluateExpressionGet(context, "#{estadistica}", estadistica.class);
    IdiomaBean msgs = (IdiomaBean) context.getApplication().
            evaluateExpressionGet(context, "#{IdiomaBean}", IdiomaBean.class);

    public PieChartModel getPieModeldoing() {
        return pieModeldoing;
    }

    public PieChartModel getPieModeldone() {
        return pieModeldone;
    }

    public PieChartModel getPieModeltodo() {
        return pieModeltodo;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public CartesianChartModel getBurndownModel() {
        return burndownModel;
    }

    public void setBurndownModel(CartesianChartModel burndownModel) {
        this.burndownModel = burndownModel;
    }

    private void createBurndownModel() {

        burndownModel = new CartesianChartModel();


        int duracion = estad.getDuracion();

        int actual[] = estad.getActual(); //Empezamos a dibujar en la grafica
        int estimado[] = estad.getEstimado();

        LineChartSeries series1 = new LineChartSeries();
        LineChartSeries series2 = new LineChartSeries();

        series1.setLabel("Estim");
        series2.setLabel("Real");

        for (int i = 0; i <= duracion; i++) {
            series1.set(i, estimado[i]);
            series2.set(i, actual[i]);
            log.log(Level.INFO, "Actual = {0}", actual[i]);
        }

        burndownModel.addSeries(series1);
        burndownModel.addSeries(series2);
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        pieModel.set("to do", estad.getTodo());
        pieModel.set("doing", estad.getDoing());
        pieModel.set("done", estad.getDone());
    }

    /**
     * Creates a new instance of burndownBean
     */
    public burndownBean() {
        createBurndownModel();
        createPieModel();
        createGeneral();
    }

    private void createGeneral() {
        HashMap<String, int[]> mapita = estad.getMapaColaboradores();
        pieModeltodo = new PieChartModel();
        pieModeldoing = new PieChartModel();
        pieModeldone = new PieChartModel();
        for (Colaborador o : estad.getColaboradorFacade().findAll()) {
            pieModeltodo.set(o.getNombre(), mapita.get(o.getNombre())[0]);
            pieModeldoing.set(o.getNombre(), mapita.get(o.getNombre())[1]);
            pieModeldone.set(o.getNombre(), mapita.get(o.getNombre())[2]);
        }
    }
}
